package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class SetMealServiceimpl implements SetMealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //开启分页
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //查询
        Page<SetmealVO> page =  (Page<SetmealVO>) setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    public void save(SetmealDTO setmealDTO) {
        //新增套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);

        //新增套餐菜品关系
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
            setMealDishMapper.insertSetmealDish(setmealDish);
        }

    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> ids) {
        log.info("批量删除套餐，ids：{}", ids);
        //判断套餐是否在起售中
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            if (setmeal.getStatus() == 1) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        //删除套餐
        setmealMapper.deleteBatchIds(ids);
        log.info("批量删除套餐，ids：{}");

        //删除套餐菜品关系
        setMealDishMapper.deleteBySetmeal(ids);
        log.info("批量删除套餐菜品关系，ids：{}");

    }

    /**
     * 根据id查询套餐和套餐菜品关系
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        SetmealVO SetmealVO = new SetmealVO();
        //查询套餐
        Setmeal Setmeal = setmealMapper.getById(id);
        log.info("查询套餐：{}", Setmeal);

        //查询套餐菜品关系
        List<SetmealDish> setmealDishes = setMealDishMapper.QueryBySetmealId(Arrays.asList(id));
        log.info("查询套餐菜品关系：{}", setmealDishes);
        //查询分类
        Category category =  categoryMapper.getById(Setmeal.getCategoryId());
        log.info("查询分类：{}", category);
        //设置数据
        BeanUtils.copyProperties(Setmeal,SetmealVO);
        SetmealVO.setSetmealDishes(setmealDishes);
        SetmealVO.setCategoryName(category.getName());
        return SetmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
        log.info("修改套餐：{}", setmealDTO);
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //修改套餐
        setmealMapper.update(setmeal);

        //删除套餐菜品关系
        setMealDishMapper.deleteBySetmeal(Arrays.asList(setmealDTO.getId()));
        log.info("删除套餐菜品关系，ids：{}");

        //重新插入套餐菜品关系
        for (SetmealDish setmealDish : setmealDTO.getSetmealDishes()) {
            setmealDish.setSetmealId(setmealDTO.getId());
            setMealDishMapper.insertSetmealDish(setmealDish);
        }
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        setmealMapper.update(Setmeal.builder().status(status).id(id).build());
    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
